package com.nanbei.lock;

import java.util.concurrent.TimeUnit;


/**
 * @ClassDescription: 死锁
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/9/27 14:51
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();

        new Thread(()->{
            synchronized (o1) {
                System.out.println("线程a获取到了o1锁");
                try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (o2) {
                    System.out.println("线程a获取到了o2锁");
                }
            }
        },"a").start();

        new Thread(()->{
            synchronized (o2){
                System.out.println("线程b获取到了o2锁");
                try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (o1){
                    System.out.println("线程b获取到了o1锁");
                }
            }
        },"b").start();
    }
}
